package org.wattdepot.cli.command;

import static org.junit.Assert.assertTrue;
import org.junit.Test;

/**
 * Tests the listPower method with various inputs for correct outputs.
 * 
 * @author Alex Kan and Kelvin Green
 * @version 2.0
 */
public class TestWattDepotListPower {

  WattDepotCommand commandInstance;
  String[] commandArray;
  String userInput;

  /**
   * Test WattDepotListPower command for 2 correct outputs and errors on 4 inputs.
   * 
   * Test (correct) power generated SIM_WAIAU_8 timestamp 2009-11-15T12:13:00.000-10:00: 8 Test
   * (correct) powerstats generated SIM_WAIAU_8 day 2009-11-15 sampling-interval 30 statistic
   * average: 7 Test (error) power generated SIM_WAIAU_8 timestamp 2009-11-1512:13:00.000-10:00:
   * Timestamp is not properly formatted Test (error) power generated SIM_WAIAU_8 timestamp
   * 2009-11-15T12:13:00.000-10:00 1: power [generated|consumed] {source} timestamp {timestamp} Test
   * (error) powerstats generated SIM_WAIAU_8 day 2009/11-15 sampling-interval 30 statistic average:
   * Day is not properly formatted. Test (error) powerstats generated SIM_WAIAU_8 day 2009-11-15
   * sampling-interval 30 statistic average 1: powerstats [generated|consumed] {source} day {day}
   * sampling-interval {minutes} " + "statistic {max|min|average}
   */

  @Test
  public void testListPower() {
    String CHECK_ERROR = "Check for error";
    WattDepotCommand.connect("http://server.wattdepot.org:8182/wattdepot/");
    commandInstance = new WattDepotListPowerCommand();
    userInput = "power generated SIM_WAIAU_8 timestamp 2009-11-15T12:13:00.000-10:00";
    commandArray = userInput.split(" ");
    assertTrue("Check for power generated SIM_WAIAU", commandInstance.doCommand(commandArray)
        .contains("8"));

    commandInstance = new WattDepotListPowerCommand();
    userInput = "power generated SIM_WAIAU_8 timestamp 2009-11-15T12:13:00.000-10:00 1";
    commandArray = userInput.split(" ");
    assertTrue(CHECK_ERROR, commandInstance.doCommand(commandArray).contains(
        "power [generated|consumed] {source} timestamp {timestamp}"));

    commandInstance = new WattDepotListPowerCommand();
    userInput = "power generated SIM_WAIAU_8 timestamp 2009-11-1512:13:00.000-10:00";
    commandArray = userInput.split(" ");
    assertTrue(CHECK_ERROR, commandInstance.doCommand(commandArray).contains(
        "Timestamp is not properly formatted"));

    commandInstance = new WattDepotListPowerStatsCommand();
    userInput =
        "powerstats generated SIM_WAIAU_8 day 2009-11-15 sampling-interval"
            + " 30 statistic average";
    commandArray = userInput.split(" ");
    assertTrue("Check for power generated SIM_WAIAU_8", commandInstance.doCommand(commandArray)
        .contains("7"));

    commandInstance = new WattDepotListPowerStatsCommand();
    userInput =
        "powerstats generated SIM_WAIAU_8 day 2009-11-15 sampling-interval"
            + " 30 statistic average 1";
    commandArray = userInput.split(" ");
    assertTrue(CHECK_ERROR, commandInstance.doCommand(commandArray).contains(
        "powerstats [generated|consumed] {source} day {day} " + "sampling-interval {minutes} "
            + "statistic {max|min|average}"));

    commandInstance = new WattDepotListPowerStatsCommand();
    userInput =
        "powerstats generated SIM_WAIAU_8 day 2009/11-15 sampling-interval"
            + " 30 statistic average";
    commandArray = userInput.split(" ");
    assertTrue(CHECK_ERROR, commandInstance.doCommand(commandArray).contains(
        "Day is not properly formatted."));
  }

}
